package mysql_utils

import (
	"gorm.io/gorm"
)

func Paginate(page int, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if page <= 0 {
			page = 1
		}
		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}
		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}

func PageNum(count int, pageSize int) (pageNum int) {
	if pageSize <= 0 {
		pageSize = 10
	}
	pageNum = count / pageSize
	if count%pageSize != 0 {
		pageNum++
	}
	return pageNum
}
